import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
  Ip,
  Req,
  Delete,
} from '@nestjs/common';
import { UserService } from './user.service';
import { LoginUserDto } from './dto/login-user.dto';
import { RegisterUserDto } from './dto/register-user.dto';
import { UpdateUserPasswordDto } from './dto/update-user-password.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../auth/roles.guard';
import { Roles } from '../auth/auth.decorator';
import { LoginUserVo } from './vo/login-user.vo';
import { DetailUserVo } from './vo/detail-user.vo';
import { CaptchaVo } from './vo/captcha.vo';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  // @Get('captcha')
  // async getCaptcha(@Ip() ip: string): Promise<CaptchaVo> {
  //   return this.userService.getCaptcha(ip);
  // }

  @Post('login')
  async login(@Body() loginUserDto: LoginUserDto): Promise<LoginUserVo> {
    return this.userService.login(loginUserDto);
  }

  @Post('register')
  async register(
    @Body() registerUserDto: RegisterUserDto,
    @Ip() ip: string,
  ): Promise<void> {
    await this.userService.register(registerUserDto);
  }

  @UseGuards(JwtAuthGuard)
  @Post('update_password')
  async updatePassword(
    @Request() req,
    @Body() updatePasswordDto: UpdateUserPasswordDto,
  ) {
    await this.userService.updatePassword(req.user.userId, updatePasswordDto);
    return { message: '密码修改成功' };
  }

  @UseGuards(JwtAuthGuard)
  @Post('update')
  async updateUser(@Request() req, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.updateUserInfo(updateUserDto);
  }

  @UseGuards(JwtAuthGuard)
  @Get('list')
  async getUserList(
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 10,
  ) {
    return this.userService.getUserList(page, limit);
  }

  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @Get('freeze/:id')
  async freezeUser(
    @Param('id') id: number,
    @Query('frozen') frozen: boolean = true,
  ) {
    await this.userService.freezeUser(id, frozen);
    return { message: frozen ? '用户已冻结' : '用户已解冻' };
  }

  @UseGuards(JwtAuthGuard)
  @Get('detail')
  async getUserDetail(@Request() req) {
    return this.userService.getUserDetail(req.user.userId);
  }

  @UseGuards(JwtAuthGuard)
  @Get(':id')
  async getUserById(@Param('id') id: number): Promise<DetailUserVo> {
    return this.userService.getUserDetail(id);
  }

  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @Delete(':id')
  async deleteUser(@Param('id') id: number) {
    await this.userService.deleteUser(id);
    return { message: '用户删除成功' };
  }
}
